// components/merchant/merchant.js
import { request} from '../../utils/requestUtils';

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    merchantId:{
      type:Number,
      value:null
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    value:"",
    columns: [],
    defaultIndex:''
  },

  /**
   * 组件初始化
   */
  attached:function(){
    this.getMerchant()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    merchantChange(event){
      const { picker, value, index } = event.detail;
      this.setData({
        value:value
      })
    },
    //商家取消选择
    merchantCancel(){
      this.triggerEvent('merchantCancel')
    },
    //选择商家
    merchantConfirm(){
      if(!this.data.value){
        wx.showToast({
          title: '请选择商家',
          duration: 2000,
          mask:true
        })
        return
      }
      this.triggerEvent('merchantConfirm',this.data.value)
    },
    //查询商家
    async getMerchant(){
      const result =  await request({
        url: '/api/bus/merchant/my/list',
        method: "GET"
    })
    //设置默认值
    if(this.properties.merchantId && result.data.data && result.data.data.length>0){
     const current =  result.data.data.find(v=>v.merchantId == this.properties.merchantId);
     const currentIndex =  result.data.data.findIndex(v=>v.merchantId == this.properties.merchantId);
     console.log('currentIndex',currentIndex)
     this.setData({
      value:current,
      defaultIndex:currentIndex==-1?0:currentIndex
    })
    }
    this.setData({
      columns : result.data.data
    })
    }
  }
})